//
//  CSnewMallViewController.m
//  ZSYH
//
//  Created by 主事丫环 on 2019/4/19.
//  Copyright © 2019 魏堰青. All rights reserved.
//

#import "CSnewMallViewController.h"
#import "GoodListModel.h"
#import "HomeGoodsCell.h"
#import "DISAordergoodViewController.h"
#import "DISgoodsDetailViewController.h"

@interface CSnewMallViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *modelArr;
@property(nonatomic,copy)NSString *type;

@property(nonatomic,strong)UIButton *odlBut;
@property(nonatomic,assign) NSInteger page;

@property(nonatomic,assign) NSInteger typepage;

@end

@implementation CSnewMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    UITextField *search = [[UITextField alloc]init];
//    [search createLeftViewImage:@"sousuo1"];
//    search.delegate = self;
//    self.navigationItem.titleView = search;
//    search.keyboardType = UIKeyboardTypeWebSearch;
//    search.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    self.page= 1;
    self.typepage= 0;

    self.title = _str;
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];


    self.modelArr = [NSMutableArray arrayWithCapacity:1];
    [self.view addSubview:self.collectionView];
    [self.collectionView reloadData];
    [self requestGoods];
    
    // Do any additional setup after loading the view from its nib.
}
-(void)leftClick{
    
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[DISAordergoodViewController class]]) {
            DISAordergoodViewController *vc =(DISAordergoodViewController *)controller;
            [self.navigationController popToViewController:vc animated:YES];
        }
        
    }
    
}

- (void)requestGoods {
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"typeId"] = _idstr;
    NSString *pp = [NSString stringWithFormat:@"%ld",(long)_page];
    param[@"pageNum"] = pp;
    if (_page == 1) {
        [self.modelArr removeAllObjects];

    }
    
    [NetWorkTool POST:getPlatProList param:param success:^(id dic) {
        
        self.modelArr = [GoodListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        
        [self.collectionView reloadData];
        self->_page == 1 ?[self->_collectionView.mj_header endRefreshing]:[self->_collectionView.mj_footer endRefreshing];

    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self->_collectionView.mj_footer endRefreshingWithNoMoreData];
        [self->_collectionView.mj_header endRefreshing];
        
    } fail:^(NSError *error) {
        [self->_collectionView.mj_footer endRefreshing];

        [self->_collectionView.mj_header endRefreshing];

    } needUser:NO];
    
    
    
}

#pragma mark - CollectionView -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _modelArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodsCell" forIndexPath:indexPath];
    
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    [cell refreshs:model];
    return cell;
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath {
    GoodListModel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    DISgoodsDetailViewController *vc = [DISgoodsDetailViewController new];
    vc.goodsID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
    
}



-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(KWIDTH/2 , 260);
        //        layout.headerReferenceSize = CGSizeMake(KWIDTH, 204);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight-kTabbarHeight) collectionViewLayout:layout];
        adjustInset(_collectionView);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor =RGBA(237, 237, 237, 1);
        
        [_collectionView registerNib:[UINib nibWithNibName:@"HomeGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"HomeGoodsCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [_collectionView reloadData];
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self requestGoods];
        }];
        
        _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            self.page += 1;
            [self requestGoods];
        }];
        
    }
    return _collectionView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
